package com.uwang.library.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.uwang.library.util.DBUtil;
import com.uwang.library.dao.MyTimerTaskDao;
import com.uwang.library.model.BeOverdueModel;
import com.uwang.library.model.BtnModel;
import com.uwang.library.util.DruidUtil;

public class MyTimerTaskDaoImpl implements MyTimerTaskDao {
	

	@Override
	public List<BeOverdueModel> getBeOverdue() {
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Connection connection = DruidUtil.getConnection();
		
		List<BeOverdueModel> beOverdueModelList = new ArrayList<>();
		try {
			String sql = "select b.id,m.user_name userName,m.user_phone userPhone,s.`name` bookName,b.borrowing_time borrowingTime,b.term,b.status from borrowing b\r\n" + 
					"left join member m on b.user_id = m.id\r\n" + 
					"left join shuji s on s.id = b.book_id\r\n" + 
					"where NOW() > b.term and b.`status` >= 1";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				BeOverdueModel beOverdueModel = new BeOverdueModel();
				beOverdueModel.setId(resultSet.getInt("id"));
				beOverdueModel.setUserName(resultSet.getString("userName"));
				beOverdueModel.setUserPhone(resultSet.getString("userPhone"));
				beOverdueModel.setBookName(resultSet.getString("bookName"));
				beOverdueModel.setTerm(resultSet.getTimestamp("term"));
				beOverdueModel.setBorrowingTime(resultSet.getTimestamp("borrowingTime"));
				beOverdueModel.setStatus(resultSet.getInt("status"));
				beOverdueModelList.add(beOverdueModel);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				resultSet.close();
				preparedStatement.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			}
		return beOverdueModelList;

	}
	
	
	
	public void setstatus(int id,int status) {
		Connection connection = DruidUtil.getConnection();
		PreparedStatement PreparedStatement;
		String sql = "UPDATE borrowing set status=? WHERE id=?";
		try {
			PreparedStatement= connection.prepareStatement(sql);
			PreparedStatement.setInt(1, status);
			PreparedStatement.setInt(2, id);
			PreparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
